package com.example.musicplayer.dialogTools;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioGroup;

import com.example.musicplayer.MusicScoreManager;
import com.example.musicplayer.R;

public class SortMusicScoreDialog extends BaseDialog {
    private OnConfirmListener mOnConfirmListener;
    private int mCheckedSortType;
    private boolean mSortReverse;
    private boolean mOnlyScored;

    public interface OnConfirmListener {
        void onConfirm(int sortType, boolean sortReverse, boolean onlyScored);
    }

    public SortMusicScoreDialog(Context context) {
        super(context, R.style.NormalDialogStyle_Bottom);
        mCheckedSortType = MusicScoreManager.SORT_DEFAULT;
        mSortReverse = false;
        mOnlyScored = false;
        MusicScoreManager manager = MusicScoreManager.getInstance();
        if (manager != null) {
            mCheckedSortType = manager.getSortType();
            mSortReverse = manager.isSortReverse();
            mOnlyScored = manager.isOnlyScored();
        }
        //init the checked button
        int resId = R.id.sort_default;
        switch (mCheckedSortType) {
            case MusicScoreManager.SORT_DEFAULT:
                resId = R.id.sort_default;
                break;
            case MusicScoreManager.SORT_LYRIC:
                resId = R.id.sort_lyric;
                break;
            case MusicScoreManager.SORT_MELODY:
                resId = R.id.sort_melody;
                break;
            case MusicScoreManager.SORT_ARRANGEMENT:
                resId = R.id.sort_arrangement;
                break;
            case MusicScoreManager.SORT_FAVOR:
                resId = R.id.sort_favor;
                break;
            case MusicScoreManager.SORT_FINAL:
                resId = R.id.sort_final;
                break;
        }
        ((RadioGroup) mDialogView.findViewById(R.id.radio_group)).check(resId);
        //sort checked button
        ((RadioGroup) mDialogView.findViewById(R.id.radio_group_sort_reverse)).check(mSortReverse ?
                R.id.sort_reverse : R.id.sort_order);
        mDialogView.findViewById(R.id.only_scored).setSelected(mOnlyScored);
    }

    public void setOnConfirmListener(OnConfirmListener listener) {
        mOnConfirmListener = listener;
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    protected View setDialogView(Context context) {
        View view = View.inflate(context, R.layout.dialog_sort_music_score, null);
        //check event
        view.findViewById(R.id.sort_default).setOnClickListener(v -> {
            mCheckedSortType = MusicScoreManager.SORT_DEFAULT;
            ((RadioGroup) mDialogView.findViewById(R.id.radio_group)).check(R.id.sort_default);
        });
        view.findViewById(R.id.sort_lyric).setOnClickListener(v -> {
            mCheckedSortType = MusicScoreManager.SORT_LYRIC;
            ((RadioGroup) mDialogView.findViewById(R.id.radio_group)).check(R.id.sort_lyric);
        });
        view.findViewById(R.id.sort_melody).setOnClickListener(v -> {
            mCheckedSortType = MusicScoreManager.SORT_MELODY;
            ((RadioGroup) mDialogView.findViewById(R.id.radio_group)).check(R.id.sort_melody);
        });
        view.findViewById(R.id.sort_arrangement).setOnClickListener(v -> {
            mCheckedSortType = MusicScoreManager.SORT_ARRANGEMENT;
            ((RadioGroup) mDialogView.findViewById(R.id.radio_group)).check(R.id.sort_arrangement);
        });
        view.findViewById(R.id.sort_favor).setOnClickListener(v -> {
            mCheckedSortType = MusicScoreManager.SORT_FAVOR;
            ((RadioGroup) mDialogView.findViewById(R.id.radio_group)).check(R.id.sort_favor);
        });
        view.findViewById(R.id.sort_final).setOnClickListener(v -> {
            mCheckedSortType = MusicScoreManager.SORT_FINAL;
            ((RadioGroup) mDialogView.findViewById(R.id.radio_group)).check(R.id.sort_final);
        });
        //sort select
        view.findViewById(R.id.sort_order).setOnClickListener(v -> {
            mSortReverse = false;
            ((RadioGroup) mDialogView.findViewById(R.id.radio_group_sort_reverse)).check(R.id.sort_order);
        });
        view.findViewById(R.id.sort_reverse).setOnClickListener(v -> {
            mSortReverse = true;
            ((RadioGroup) mDialogView.findViewById(R.id.radio_group_sort_reverse)).check(R.id.sort_reverse);
        });
        view.findViewById(R.id.only_scored).setOnClickListener(v -> {
            mOnlyScored = !mOnlyScored;
            v.setSelected(mOnlyScored);
        });
        //confirm event
        view.findViewById(R.id.confirm).setOnClickListener(v -> {
            if (mOnConfirmListener != null) {
                mOnConfirmListener.onConfirm(mCheckedSortType, mSortReverse, mOnlyScored);
            }
            SortMusicScoreDialog.this.dismiss();
        });
        return view;
    }

    @Override
    protected void setDialogLayout(Context context) {
        Window dialogWindow = mDialog.getWindow();
        if (dialogWindow != null) {
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.gravity = Gravity.BOTTOM;
            dialogWindow.setAttributes(lp);
        }
    }
}